package cn.echo.ecc.util;

import cn.echo.ecc.dto.LoginContextDto;
import cn.echo.ecc.enums.GlobalExceptionCode;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 获取登录人信息的工具类
 */
public class LoginContextUtil {

    public static LoginContextDto getLoginContext() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AssertUtil.isTrue(authentication.isAuthenticated(), GlobalExceptionCode.FAIL);
        LoginContextDto loginContextDto = JSON.parseObject(authentication.getPrincipal().toString(), LoginContextDto.class);
        return loginContextDto;
    }
}
